package com.taobao.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.taobao.dao.CommonSerDao;
import com.taobao.dao.ReceiveDao;
import com.taobao.pojo.Config;
import com.taobao.pojo.ConfirmTime;
import com.taobao.pojo.HttpContext;
import com.taobao.pojo.State;
import com.taobao.pojo.Task;

public class ReceiveSerDaoImpl extends CommonSerDao implements ReceiveDao {

    @Override
    public Object process(HttpContext context) throws Exception {
        List<Task> tasks = new ArrayList<Task>();
        String xml = context.getHtml();

        Document doc = Jsoup.parse(xml);
        Elements es = doc.getElementsByTag("TasksIng");
        for (Iterator<Element> iterator = es.iterator(); iterator.hasNext();) {
            Task task = new Task();
            Element el = (Element) iterator.next();

            String tid = el.getElementsByTag("tid").text();
            String fbd = el.getElementsByTag("fbd").text();
            String username = el.getElementsByTag("username").text();
            String itemPrice = el.getElementsByTag("itemPrice").text();
            String ftime = el.getElementsByTag("ftime").text();
            String jtime = el.getElementsByTag("jtime").text();
            String qq = el.getElementsByTag("qq").text();
            String jsmalluser = el.getElementsByTag("jsmalluser").text();
            String status = el.getElementsByTag("status").text();
            String hpWay = el.getElementsByTag("hpWay").text();
            String itemUrl = el.getElementsByTag("itemUrl").text();
            String hpWord = el.getElementsByTag("hpWord").text();
            String seller = el.getElementsByTag("seller").text();

            task.setId(Long.parseLong(tid));
            task.setPublishPoint(Double.parseDouble(fbd));
            task.setName(username);
            task.setPrice(Double.parseDouble(itemPrice));
            task.setPublishTime(ftime);
            task.setReceiveTime(jtime);
            task.setQq(qq);
            task.setSmallUser(jsmalluser);
            task.setState(new State(Integer.parseInt(status)));
            task.setConfirmTime(new ConfirmTime(Integer.parseInt(hpWay)));
            task.setTbUrl(itemUrl);
            task.setHpWord(hpWord);
            task.setTbName(seller);

            tasks.add(task);
        }

        return tasks;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Task> getReceiveTasksIng() throws Exception {
        List<Task> tasks = new ArrayList<Task>();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("AUserName", Config.userName);
        int index = 0;
        while (true) {
            params.put("APageIndex", index + "");
            List<Task> taskList = (List<Task>) postRequest("JTasksIng", params);
            if (taskList.size() == 0) {
                break;
            } else {
                tasks.addAll(taskList);
            }
            index++;
        }
        return tasks;
    }
}
